<template>
  <div class="bg">
    <div class="container-small">
      <form action="">
        <div class="reglogin step1">
          <router-link to="/">
            <div class="logo"></div>
          </router-link>
          <div class="title">注册科沃斯账号</div>
          <div class="item">
            <input
              type="text"
              class="u-ipt"
              name="mobile"
              v-model="registerData.phone"
              placeholder="请输入11位手机号"
              @blur="checkNum"
            />
            <div class="tips error" v-show="checknum">{{ numMsg }}</div>
          </div>
          <div class="item">
            <input
              type="password"
              class="u-ipt"
              name="password"
              v-model="psd1"
              placeholder="请输入密码"
              autocomplete="“off”"
              @blur="psd1Check"
            />
            <div class="tips error" v-show="checkpsd1">{{ psdMsg }}</div>
          </div>
          <div class="item">
            <input
              type="password"
              class="u-ipt"
              name="password"
              v-model="registerData.password"
              placeholder="请确认密码"
              autocomplete="“off”"
              @blur="psd2Check"
            />
            <div class="tips error" v-show="checkpsd2">{{ psdMsg }}</div>
          </div>
          <div class="item button">
            <button
              @click.prevent="registerNow"
              :class="{ red: able }"
              :disabled="!able"
            >
              立即注册
            </button>
          </div>
          <div class="item text">
            <label>
              <input type="checkbox" name="agreement" v-model="checked" />
              我已阅读并同意
              <a href="#" target="_blank">
                <b>会员注册协议</b>
              </a>
            </label>
          </div>
          <div class="item text">
            <span
              >已有账号？
              <router-link to="/login">立即登录</router-link>
            </span>
          </div>
        </div>
      </form>
    </div>
    <!-- 底部 -->
    <Footer></Footer>
    <!-- 侧边咨询客服按钮 -->
    <consult-serve></consult-serve>
  </div>
</template>

<script>
import { mapActions, mapMutations } from "vuex";
import { register, login } from "../assets/js/request.js";
import Footer from "../components/Footer.vue";
import ConsultServe from "../components/ConsultServe.vue";
export default {
  data() {
    return {
      imgList: {
        qrcode1: require("../assets/image/login/700000001.png"),
        consult: require("../assets/image/login/ia_700000014.png"),
        szicbok: require("../assets/image/login/szicbok.gif"),
        consult2: require("../assets/image/login/ia_700000002.png"),
        consult3: require("../assets/image/login/ia_700000004.jpg"),
        logoImg: require("../assets/image/login/default.png"),
      },
      registerData: {
        name: "",
        phone: "",
        password: "",
        type: 1,
      },
      psd1: "",
      checknum: false,
      checkpsd1: false,
      checkpsd2: false,
      numMsg: "请输入手机号!",
      psdMsg: "请输入密码!",
      checked: false,
    };
  },
  components: {
    Footer,
    ConsultServe,
  },
  methods: {
    ...mapActions("cart", ["getCartprod"]),
    ...mapMutations("cart", ["getUserId"]),
    //验证手机号码
    checkNum() {
      let reg =
        /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
      let flag = reg.test(this.registerData.phone);
      if (this.registerData.phone == "") {
        this.numMsg = "请输入手机号";
        this.checknum = true;
        return false;
      } else if (flag) {
        this.checknum = false;
        return true;
      } else {
        this.numMsg = "请输入正确的手机号";
        this.checknum = true;
        return false;
      }
    },
    // 验证 密码
    psd1Check() {
      if (this.psd1) {
        this.checkpsd1 = false;
        return true;
      } else {
        this.psdMsg = "请输入密码!";
        this.checkpsd1 = true;
        return false;
      }
    },
    psd2Check() {
      if (this.registerData.password) {
        if (this.psd1 == this.registerData.password) {
          this.checkpsd2 = false;
          return true;
        } else {
          this.psdMsg = "密码输入不一致!";
          this.checkpsd2 = true;
          this.checkpsd1 = true;
          return false;
        }
      } else {
        this.psdMsg = "请输入密码!";
        this.checkpsd2 = true;
      }
    },
    async registerNow() {
      this.registerData.name = this.registerData.phone;
      try {
        let res = await register(this.registerData);
        login({
          phone: res.phone,
          password: res.password,
        }).then((res) => {
          this.getUserId(res.id);
          sessionStorage.setItem("token", res.token);
          sessionStorage.setItem("phone", res.phone);
          this.getCartprod();
          if (this.$route.isPath) {
            this.$router.push(this.$route.isPath);
          } else {
            this.$router.push("/");
          }
        });
      } catch (error) {
        this.checknum = true;
        this.numMsg = "该手机号已注册";
      }
    },
  },
  computed: {
    able() {
      return (
        !this.checknum &&
        !this.checkpsd1 &&
        !this.checkpsd2 &&
        this.psd1 &&
        this.registerData.phone &&
        this.registerData.password &&
        this.checked
      );
    },
  },
};
</script>

<style lang="scss">
html {
  background-color: #f2f2f2;
  body {
    line-height: 1.5 !important;

    .bg {
      background-color: #f2f2f2;
      .container-small {
        margin: 60px auto;
        width: 860px;
        background-color: #fff;
        position: relative;

        .reglogin {
          width: 360px;
          margin: 0 auto;
          padding: 60px 0;

          .logo {
            width: 100%;
            height: 50px;
            text-align: center;

            &::before {
              content: "";
              display: inline-block;
              background: url("../assets/image/login/default.png") center
                no-repeat;
              background-size: contain;
              width: 240px;
              height: 50px;
            }
          }

          .title {
            padding-top: 30px;
            padding-bottom: 50px;
            text-align: center;
            font-size: 24px;
            color: #666;
          }

          .item {
            position: relative;
            margin-bottom: 25px;
            .u-ipt {
              background-color: #fff;
              color: #888;
              font-size: 14px;
              width: 100%;
              height: 50px;
              padding: 10px;
              border: 1px solid #e0e0e0;
              outline: 0;
            }
            .tips {
              position: absolute;
              left: 0;
              bottom: -20px;
              width: 100%;
              height: 20px;
              color: #ee0a3b;
            }
            .tips.error::before {
              display: inline-block;
              vertical-align: middle;
              content: "";
              width: 18px;
              height: 20px;
              background: url(../assets/image/register/icons-201703.png) -66px 0
                no-repeat;
            }
            img {
              border: 1px solid #e0e0e0;
            }
            .col-md-7 {
              width: 58%;
            }
            .col-md-4 {
              width: 33%;
              height: 50px;
              img {
                width: 100%;
                height: 100%;
              }
            }
          }

          .item.button {
            margin-top: 20px;

            button {
              padding: 0;
              width: 100%;
              height: 50px;
              line-height: 50px;
              font-weight: 400;
              cursor: default;
              color: #888;
            }
            button.red {
              background-color: #ee0a3b;
              color: #fff;
              border: 1px solid transparent;
            }
          }

          .item.text {
            font-size: 12px;
            color: #6d6d6f;
            padding-bottom: 5px;

            label {
              color: #6d6d6f;
              margin-bottom: 0;

              input {
                vertical-align: middle;
                margin-right: 2px;
              }

              a {
                color: #333;
                text-decoration: none;
              }
            }

            span {
              width: 100%;
              display: inline-block;
              text-align: center;
              padding-top: 70px;

              a {
                color: #333;
                text-decoration: none;

                &:hover {
                  color: #ee0a3b;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>